<template>
  <div class="map_wrapper">
    <div class="hd">
      <h2 class="fl">位置周边</h2>
      <span class="fr list tab">
        <span
          class="span"
          :class="{ 'on': current == index }"
          v-for="(item, index) in list"
          :key="index"
          @click="searchPeriphery(item, index)"
        >
          {{ item }}
        </span>
      </span>
    </div>

    <div class="contentMap">
      <div class="result-box">
        <div class="name">
          <div class="name-title">
            银行
            <span class="unds-project-situation">规划情况</span>
          </div>
        </div>
        <div class="result-wrapper">
          <div
            class="result-item"
            v-for="(item, index) in resultList"
            :key="index"
          >
            <div class="title-name" @click="showInfo(item)">
              <span class="label">{{ en[index] }}</span>
              {{ item.title }}
            </div>
            <div class="cont">{{ item.address }}</div>
          </div>
        </div>
      </div>
      <div id="map"></div>
    </div>
  </div>
</template>

<script>
import { defineComponent, reactive, toRefs, onMounted, nextTick } from "vue";
import BMapGL from "BMap";
import bg from "@/assets/images/yy.png";
export default defineComponent({
  props:
    ['markers']
  ,
  setup(props) {
    console.log(props.markers)
    const data = reactive({
      list: [
        "公交",
        "地铁",
        "医院",
        "银行",
        "休闲娱乐",
        "购物",
        "餐饮",
        "运动健身",
      ],
      map: null,
      resultList: [],
      en: [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z",
      ],
      current: 0,
    });

    const newMap = () => {
      data.map = new BMapGL.Map("map", { minZoom: 13, maxZoom: 18 });
      data.map.centerAndZoom(props.markers[0].city, 12);
      data.map.enableScrollWheelZoom();
      searchPeriphery("公交",0);
    };

    const pointe = () => {
      let pointNumber = new BMap.Point(props.markers[0].longitude, props.markers[0].latitude);

      const content = `<div class="xq-box">${props.markers[0].house_title}(均价)</div>`;
      var label = new BMap.Label(content);
      label.setStyle({
        background: "transparent",
        border: "none",
        cursor: "pointer",
      });

      const myIcon = new BMap.Icon(bg, new BMap.Size(0, 0));
      let markers = new BMap.Marker(pointNumber, {
        icon: myIcon,
      });
      data.map.addOverlay(markers);
      markers.setLabel(label);
    };

    const searchPeriphery = (type, index) => {
      data.current = index;
      data.map.clearOverlays();
      pointe();
      console.log(props.markers[0].longitude, props.markers[0].latitude)
      var mPoint = new BMapGL.Point(props.markers[0].longitude, props.markers[0].latitude);
      var circle = new BMapGL.Circle(mPoint, 2000, {
        fillColor: "black",
        strokeWeight: 1,
        fillOpacity: 0.3,
        strokeOpacity: 0.3,
      });
      console.log(mPoint)
      circle._name = "circle";
      data.map.addOverlay(circle);
      var local = new BMapGL.LocalSearch(data.map, {
        renderOptions: {
          map: data.map,
          selectFirstResult: false,
          autoViewport: true,
        },
        onSearchComplete: function (LocalResultPoi) {
          data.resultList = LocalResultPoi.Br;
        },
      });
      local.searchNearby(type, mPoint, 2000);
    };

    const showInfo = (item) => {
      var opts = {
        width: 100, // 信息窗口宽度
      };
      var infoWindow = new BMap.InfoWindow(
        `<p style='font-size:14px;margin-bottom:10px'>${item.title}</p><p style='font-size:14px;color:#888'>${item.address}</p>`,
        opts
      );
      item.marker.openInfoWindow(infoWindow);
    };

    const removeOverlay = (name) => {
      let all = data.map.getOverlays();
      all.forEach((item) => {
        if (item._name == name) {
          data.map.removeOverlay(item);
        }
      });
    };

    onMounted(() => {
      nextTick(() => {
        newMap();
      });
    });

    return {
      ...toRefs(data),
      searchPeriphery,
      showInfo,
    };
  },
});
</script>


<style lang="scss">
.map_wrapper {
  .xq-box {
    color: #fff;
    background: #3f85ff;
    padding: 6px 10px;
    border-radius: 12px;
    position: relative;
    box-shadow: 3px 3px 3px #848484;
    transition: all 0.3s;
    // border: 1px solid #fff;
    span {
      background: #fff;
      border-radius: 4px;
      color: #3f85ff;
      padding: 0 4px;
    }
    &::before {
      content: "";
      position: absolute;
      bottom: -17px;
      left: 17px;
      border-bottom: 10px solid transparent;
      border-top: 10px solid #3f85ff;
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
      transition: all 0.3s;
    }
    &:hover {
      &::before {
        border-top: 10px solid #e03236;
      }
      span {
        color: #e03236;
      }
      background: #e03236;
      z-index: 9999999 !important;
    }
  }
  .hd {
    height: 50px;
    border-bottom: 1px solid #efefef;
    display: flex;
    margin: 0;

    h2 {
      font-size: 24px;
      font-weight: bold;
      line-height: 24px;
    }

    .tab {
      .span {
        display: inline-block;
        height: 24px;
        line-height: 24px;
        margin-left: 33px;
        color: #9b9b9b;
        cursor: pointer;
        font-size: 14px;
        &:hover {
          color: #3f85ff;
        }
      }
      .on {
        color: #3f85ff;
      }
    }
  }
  .contentMap {
    width: 1200px;
    height: 510px;
    position: relative;
    #map {
      width: 1200px;
      height: 510px;
    }
    .result-box {
      position: absolute;
      top: 23px;
      bottom: 23px;
      right: 23px;
      width: 310px;
      z-index: 99;

      background-color: #fff;
      box-shadow: 0 0 7px ragb(0, 0, 0, 0.5);
      .name {
        line-height: 39px;
        text-align: center;
        font-size: 14px;
        font-weight: 700;
        color: #fff;
        text-align: left;
        background-color: #568fe3;
        .name-title {
          padding-left: 20px;
          height: 40px;
          line-height: 40px;
          .unds-project-situation {
            background: #fff;
            color: #568fe3;
            border-radius: 2px;
            float: right;
            display: block;
            height: 30px;
            line-height: 30px;
            margin-bottom: 5px;
            margin-top: 5px;
            margin-right: 5px;
            padding: 0 5px;
            cursor: pointer;
          }
        }
      }
      .result-wrapper {
        overflow-y: auto;
        height: 426px;
        padding: 20px 20px;
        padding-left: 50px;
        .result-item {
          margin-bottom: 20px;
          .title-name {
            font-size: 14px;
            font-weight: 700;
            color: #333;
            cursor: pointer;
            .label {
              display: inline-block;
              width: 25px;
              height: 25px;
              border-radius: 50%;
              font-size: 12px;
              line-height: 25px;
              text-align: center;
              background: #eb2d2d;
              color: #fff;
              margin-left: -34px;
            }
          }
          .cont {
            color: #888;
            font-size: 0.85em;
            line-height: 18px;
            margin-top: 8px;
          }
        }
      }
    }
  }
}
</style>